package com.sisqorap.puzzelalarm;

import com.sisqorap.puzzelalarm.R;

import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuInflater;
import android.view.View;
import android.widget.AnalogClock;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TimePicker;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuItem;

public class PuzzelAlarm extends Activity {
	private Toast mToast;

	private int iHour;
	private int iMinute;

	static final int TIME_DIALOG_ID = 0;
	static final int WU_DIALOG_ID = 1;
	
	private Alarm alarm;
	
	private PopupWindow aboutPop;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	    setContentView(R.layout.main);
		
	    alarm = new Alarm(PuzzelAlarm.this);
	    
	    Button mPickTime = (Button) findViewById(R.id.set_firstAlarm);
	    // add a click listener to the button
	    mPickTime.setOnClickListener(new View.OnClickListener() {
	        public void onClick(View v) {
	            showDialog(TIME_DIALOG_ID);
	        }
	    });
	    
	    // display the current date
	    updateDisplay();
	    Button clearTimes = (Button)findViewById(R.id.btnClear);
	    clearTimes.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				//Cancel First alarm
				alarm.removeFirstAlarm();
	            
	            //Cancel Wake-up alarm
	            alarm.removeWakeUpAlarm();
	            updateDisplay();
			}
		});
	}
    
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		Settings setting = new Settings(this);
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_menu, menu);
	    MenuItem mItem = menu.findItem(R.id.sNPieces4);
	    switch(setting.getNumberOfPuzzlePieces()){
	    case 4:
	       	mItem = menu.findItem(R.id.sNPieces4);
	       	break;
	    case 5:
	       	mItem = menu.findItem(R.id.sNPieces5);
	       	break;
	    case 6:
	       	mItem = menu.findItem(R.id.sNPieces6);
	       	break;
	    case 7:
	       	mItem = menu.findItem(R.id.sNPieces7);
	       	break;
	    case 8:
	       	mItem = menu.findItem(R.id.sNPieces8);
	       	break;
       	default:
       		mItem = menu.findItem(R.id.sNPieces4);
	       	break;
	    }
	    mItem.setChecked(true);
	    mItem = menu.findItem(R.id.sSnoozeTime5);
	    switch(setting.getSnoozeTime()){
	    	case 5:
	    		mItem = menu.findItem(R.id.sSnoozeTime5);
	    		break;
	    	case 10:
	    		mItem = menu.findItem(R.id.sSnoozeTime10);
	    		break;
	    	case 15:
	    		mItem = menu.findItem(R.id.sSnoozeTime15);
	    		break;
	    }
	    mItem.setChecked(true);
	    return true;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu){
		return super.onPrepareOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
    	Settings setting = new Settings(this);
	    switch (item.getItemId()) {
	        case R.id.sNPieces4:
	        case R.id.sNPieces5:
	        case R.id.sNPieces6:
	        case R.id.sNPieces7:
	        case R.id.sNPieces8:
	        	item.setChecked(true);
	        	//Log.i("Selected", item.getItemId() + "");
	        	setting.setNumberOfPuzzlePieces(Short.parseShort(item.getTitle() + ""));
	            break;
	        case R.id.sSnoozeTime5:
	        case R.id.sSnoozeTime10:
	        case R.id.sSnoozeTime15:
	        	item.setChecked(true);
	        	setting.setSnoozeTime(Short.parseShort((item.getTitle() + "").split(" ")[0]));
	        	break;
	        case R.id.mUnHideClock:
	        	AnalogClock aClock = (AnalogClock)findViewById(R.id.currentTime);
	        	if(aClock.getVisibility() == View.VISIBLE){
	        		aClock.setVisibility(View.GONE);
	        		item.setTitle(R.string.mUnHideClock);
	        	}else{
	        		aClock.setVisibility(View.VISIBLE);
	        		item.setTitle(R.string.mHideClock);
	        	}
	        	break;
	        case R.id.mAbout:
	        	View aboutView = View.inflate(this, R.layout.about, null);
	        	Button btnOk = (Button)aboutView.findViewById(R.id.btnOK);
	    	    btnOk.setOnClickListener(new View.OnClickListener() {
	    			public void onClick(View v) {
	    				aboutPop.dismiss();
	    			}
	    		});
	        	aboutPop = new PopupWindow(aboutView, LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT, true);
	        	aboutPop.setContentView(aboutView);
	        	aboutPop.showAtLocation(aboutView, Gravity.CENTER, 0, 0);
	        	break;
	    }
	    return true;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
	    switch (id) {
	    case TIME_DIALOG_ID:
	    	iHour = alarm.getFirstAlarmHour();
	    	Log.v("Hour", iHour + "");
	    	iMinute = alarm.getFirstAlarmMinute();
	    	Log.v("Minute", iMinute + "");
	        return new TimePickerDialog(this, mTimeSetListener, iHour, iMinute, true);
	    case WU_DIALOG_ID:
	    	iHour = alarm.getWakeUpAlarmHour();
	    	iMinute = alarm.getWakeUpAlarmMinute();
	    	return new TimePickerDialog(this, wuTimeSetListener, iHour, iMinute, true);
	    }
	    return null;
	}
	
	// the callback received when the user "sets" the time in the dialog
	private TimePickerDialog.OnTimeSetListener mTimeSetListener =
	    new TimePickerDialog.OnTimeSetListener() {
	        public void onTimeSet(TimePicker view, int hourOfDay, int minuteSet) {
        		String alarmTime = alarm.setFirstAlarm(hourOfDay, minuteSet);
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(PuzzelAlarm.this, alarmTime, Toast.LENGTH_LONG);
                mToast.show();
	            updateDisplay();
	        }
	    };
	    
	private TimePickerDialog.OnTimeSetListener wuTimeSetListener =
	    new TimePickerDialog.OnTimeSetListener() {
	        public void onTimeSet(TimePicker view, int hourOfDay, int minuteSet) {
                String alarmTime = alarm.setWakeUpAlarm(hourOfDay, minuteSet);
                // Tell the user about what we did.
                if (mToast != null) {
                    mToast.cancel();
                }
                mToast = Toast.makeText(PuzzelAlarm.this, alarmTime, Toast.LENGTH_LONG);
                mToast.show();
	            updateDisplay();
	        }
	    };

	// updates the time we display in the TextView
	private void updateDisplay() {
		Button clearBtn = (Button)findViewById(R.id.btnClear);
		Button fAlarmBtn = (Button)findViewById(R.id.set_firstAlarm);
		Button wuAlarmBtn = (Button)findViewById(R.id.set_WUAlarm);
		if(alarm.isFirstAlarmSet()){
			fAlarmBtn.setText(getString(R.string.set_firstAlarm) + "\n" + alarm.getFirstAlarmTime());
		    wuAlarmBtn.setEnabled(true);
		    wuAlarmBtn.setOnClickListener(new View.OnClickListener() {
    	        public void onClick(View v) {
    	            showDialog(WU_DIALOG_ID);
    	        }
    	    });
	        if(alarm.isWakeUpAlarmSet()){
	        	wuAlarmBtn.setText(getString(R.string.set_WUAlarm) + "\n" + alarm.getWakeUpAlarmTime());
	        }else{
	        	wuAlarmBtn.setText(getString(R.string.set_WUAlarm));
	        }
	        clearBtn.setEnabled(true);
        }else{
        	fAlarmBtn.setText(getString(R.string.set_firstAlarm));
        	wuAlarmBtn.setText(getString(R.string.set_WUAlarm));
        	wuAlarmBtn.setEnabled(false);
        	clearBtn.setEnabled(false);
        }
	}
}